<template >
    <div class="back">
        <div class="login">
            <h3>叩丁狼商品管理系统</h3>
            <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" class="demo-ruleForm">
                <el-form-item prop="username">
                    <el-input v-model="ruleForm.username" placeholder="请输入账号">
                        <template #prefix>
                            <el-icon :size="20">
                                <User />
                            </el-icon>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input v-model="ruleForm.password" type="password" autocomplete="off" placeholder="请输入密码"
                        show-password>
                        <template #prefix>
                            <el-icon :size="20">
                                <Lock />
                            </el-icon>
                        </template></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm(ruleFormRef)" v-noshake>登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>


</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import type { FormInstance } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { loginAPI } from './api'
import router from '@/router'


const ruleFormRef = ref<FormInstance>()



const validatePass = (rule: any, value: string, callback: any) => {
    if (value === '') {
        callback(new Error('请输入账号'))
    } else {
        if (ruleForm.password !== '') {
            if (!ruleFormRef.value) return
            ruleFormRef.value.validateField('password', () => null)
        }
        callback()
    }
}
const validatePass2 = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('请输入密码'))
    } else {
        callback()
    }
}

const ruleForm = reactive({
    username: 'wei',
    password: '123456',
})

// 判断是否激活检验
const rules = reactive({
    username: [{ validator: validatePass, trigger: 'blur' }],
    password: [{ validator: validatePass2, trigger: 'blur' }],
})


// 点击提交按钮时函数
const submitForm = (formEl: FormInstance | undefined) => {
    // 判断传入dom的数据
    if (!formEl) return
    formEl.validate((valid) => {
        if (valid) {
            //valid是true代表数据校验通过，发请求
            loginAPI(ruleForm).then((res) => {
                if (res.code === 200) {
                    localStorage.setItem("token", res.data.tokenHead + res.data.token)

                    ElMessage.success("登录成功")

                    router.push('/index')
                }
            })
        } else {
            console.log('error submit!')
            // 如果校验失败要返回false
            ElMessage.error('请输入正确的账号密码')
            return false
        }
    })
}



</script>

<style scoped lang="less">
.back {
    width: 100%;
    height: 100%;
    background-color: #2d3a4b;
    overflow: hidden;
    position: relative;

    .login {
        width: 400px;
        background-color: #2d3a4b;
        padding: 10px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);


        h3 {
            padding: 20px 0;
            color: #fff;
            text-align: center;
        }

        :deep(.el-input__inner) {
            height: 50px;
            width: 100%;

        }

        :deep(.el-button) {
            width: 100%;
            height: 40px;
        }

        :deep(.el-input__wrapper) {
            background-color: #283443;
            border: 1px solid #3e4957;
            box-shadow: none;
        }



    }
}
</style>